# -*- coding:utf-8 -*-
#作者：琴弦上的宇宙
#时间：2021年10月22日

import os
from common import *
from openCv import *

class scPveSelectTreasury(myScene):
    def __init__(self,bValid):
        self.bValid=bValid
        self.startPos=[0,0]
        self.name='scPveSelectTreasury'
        self.path='resource/mercenary/pve/scPveSelectTreasury'
        self.tagPng=[myPng(self.path,png) for png in os.listdir(self.path) if png.startswith('tag')]
        self.funcPng=[myPng(self.path,png) for png in os.listdir(self.path) if png.startswith('func')]
        self.funcWhitePng=[myPng(self.path,png) \
            for png in os.listdir(self.path) if png.startswith('funcWhite')]
        self.funcBlackPng=[myPng(self.path,png) \
            for png in os.listdir(self.path) if png.startswith('funcBlack')]

    def enable(self):
        self.bValid=True

    def dis(self):
        self.bValid=False

    def isOwn(self,background):
        if self.bValid:
            tagList={}
            for tag in self.tagPng:
                bFind,x,y,w,h=bFindInBackground(background,tag,0.90)
                if bFind:
                    tagList[tag.name]=(x,y,w,h)
            if ('tagTreasury' in tagList):
                return True
            else:
                return False
        else:
            return False

    def proc(self,background):

        #循环
        bg=background
        while True:

            #寻找起始位置
            if self.startPos[0]<10:
                for func in self.funcPng:
                    if (func.name=='funcArrow'):
                        bFind,x,y,w,h=bFindInBackground(bg,func,0.90)
                        if bFind:
                            self.startPos[0]=x+w/2
                            self.startPos[1]=y+h/2
                            break

            #优先寻找白名单
            bFindMust=False
            for func in self.funcWhitePng:
                bFind,x,y,w,h=bFindInBackground(bg,func,0.90)
                if bFind:
                    moveAndClick(x+w/2,y+h/2,2)
                    bFindMust=True
                    break

            #然后寻找被动
            bFindPassive=False
            if not bFindMust:
                for func in self.funcPng:
                    if (func.name=='funcPassive'):
                        bFind,x,y,w,h=bFindInBackground(bg,func,0.60)
                        if bFind:
                            moveAndClick(x+w/2,y+h/2,2)
                            bFindPassive=True
                            break

            #最后寻找随机宝藏
            rand=[150,360,570]
            if not bFindMust and not bFindPassive:
                moveAndClick(self.startPos[0]+\
                    rand[random.randint(0,2)],self.startPos[1],2)

            #排除黑名单
            findCnt=0
            while True:
                bg=SaveScreen()
                bFindDis=False
                for func in self.funcBlackPng:
                    bFind,x,y,w,h=bFindInBackground(bg,func,0.80)
                    if bFind:
                        bFindDis=True
                        break
                if not bFindDis or findCnt>5:
                    break
                else:
                    findCnt+=1
                    moveAndClick(self.startPos[0]+\
                        rand[random.randint(0,2)],self.startPos[1],2)

            #确定
            bg=SaveScreen()
            for func in self.funcPng:
                if (func.name=='funcGet') or \
                    (func.name=='funcReplace') or \
                    (func.name=='funcHold'):
                    bFind,x,y,w,h=bFindInBackground(bg,func,0.90)
                    if bFind:
                        moveAndClick(x+w/2,y+h/2,2)
                        bg=SaveScreen()
                        if not self.isOwn(bg):
                            MyGui.gRound+=1
                        return True

            #重新截图判断
            bg=SaveScreen()
            if not self.isOwn(bg):
                return False
